﻿package artibella.media.audio 
{
	import flash.media.SoundMixer;
	import flash.utils.ByteArray;

	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class AudioSpectrum
	{
		public static const BOTH	:String = "both";
		public static const LEFT	:String = "left";
		public static const RIGHT	:String = "right";

		private var _minValue:int;
		private var _maxValue:int;
		
		private var _fluxAudio:ByteArray;
		
		private var _part:String;

		public function AudioSpectrum( part:String = BOTH ) 
		{
			_part = part;
			
			switch( _part )
			{
				case LEFT : 
					_minValue = 0;
					_maxValue = 255;
					break;
				case RIGHT: 
					_minValue = 256;
					_maxValue = 512;
					break;
				default :
					_minValue = 0;
					_maxValue = 512;
			}
			
			_fluxAudio = new ByteArray();
		}
		
		// - - -
		
		public function getSpectrum():Array 
		{
			SoundMixer.computeSpectrum( _fluxAudio );

			var amplitude:Number;
			var tab:Array = new Array();
			
			for (var i:int = _minValue; i < _maxValue; i++) {
			
				amplitude = _fluxAudio.readFloat();
				tab.push( amplitude );
			}
			
			return tab;
		}
	
	}
}